package com.wpw.web.work.rifit;

import com.alibaba.fastjson.JSONObject;
import com.wpw.web.util.HttpUtil;

import java.io.IOException;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

/**
 * @Description:
 * 获取所有的sku的逻辑skuid
 * pids.txt是记录的skuId，一行一个
 * 输出sku对应裸机sku，以分隔符分隔，可以直接复制到Excel中
 * @ClassName: PIdGet
 * @CreateDate: 2020-03-27 23:02
 * @author: wupengwei
 */
public class PIdGet implements Serializable {

    public static void main(String[] args) {
        List<Long> ids = getIdFromFile();
        Map<Long, Long> idMap = new LinkedHashMap<>();
        int i = 0;
        for (Long id : ids) {
            i++;
            if (i % 50 == 0) {
                System.out.println(i + "个");
            }
            Long mapId = getPId(id);
            //一致的不改
            if (!id.equals(mapId)) {

                idMap.put(id, mapId);
            }
        }

        int count = 0;
        for (Long pid : idMap.keySet()) {
            Long mapId = idMap.get(pid);
            if (pid.equals(mapId)) {
                count ++;
                System.out.println(pid + "\t" + "一致");
            } else {

                System.out.println(pid + "\t" + mapId);
            }
        }
        System.out.println("一致的有" + count);
    }

    public static List<Long> getIdFromFile() {

        List<Long> pIds = new ArrayList<>();


        String filePath = "/Users/wupengwei/Desktop/改装脚本/pids.txt";
        Path path = Paths.get(filePath);
        try {
            List<String> codes = Files.readAllLines(path, StandardCharsets.UTF_8);
            int totalSize = codes.size();
            System.out.println("共" + totalSize + "条数据");

            for (String code : codes) {
                pIds.add(Long.parseLong(code));
            }



        } catch (IOException e) {
            e.printStackTrace();
        }

        return pIds;

    }

    public static Long getPId(Long id) {
        Map<String, Object> param = new HashMap<>();
        String url = "http://erp.edianzu.cn/api/queryNotOkrSku?productId=" + id;
        String post = HttpUtil.post(url, param);
        JSONObject jsonObject = JSONObject.parseObject(post, JSONObject.class);
        Long data = jsonObject.getLong("data");
        return data;
    }
}